package com.nyist.threadCase2;

import cn.hutool.core.util.IdUtil;
import com.nyist.thread.NyistThreadUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 1.
 *
 * @ClassName BuyTicket
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/08/16:34
 */
@Slf4j
public class BuyTicket implements Runnable {
    static Object lockObj = new Object();
    private static int ticket = 100;
    Lock lock = new ReentrantLock();

    public static void main(String[] args) {
        BuyTicket buyTicket = new BuyTicket();
        Thread t1 = new Thread(buyTicket, "一号1111");
        Thread t2 = new Thread(buyTicket, "二号2222");
        Thread t3 = new Thread(buyTicket, "三号3333");
        // t1.setPriority(1);
        // t2.setPriority(5);
        // t3.setPriority(9);
        t1.start();
        t2.start();
        t3.start();
    }


    @Override
    public void run() {
        // lock.lock();
        synchronized (lockObj) {
            while (ticket > 0) {
                NyistThreadUtils.sleep(100);
                log.info("=====>>>当前线程为:{}, 正在卖第 {} 号票, 编码为:{}", NyistThreadUtils.getThreadName(), ticket, IdUtil.fastSimpleUUID());
                ticket--;
            }
        }

        // lock.unlock();
    }
}
